package com.szty.mod_jyzs.ui.pop

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.drawable.GradientDrawable
import android.widget.LinearLayout
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.TextView
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.noober.background.drawable.DrawableCreator
import com.sjh.baseutils.color
import com.sjh.baseutils.loge
import com.sjh.lib_app.adapter.BaseAdapter
import com.sjh.lib_app.adapter.DividerItemDecoration
import com.sjh.lib_app.popup.BaseBottomPopup
import com.szty.mod_jyzs.R
import com.szty.mod_jyzs.databinding.JyzsItemColorSelectBinding

/**
 *@author : created by SunJH
 *时间：2021/8/5 000517
 *作用：底部设置笔属性的弹窗
 */
@SuppressLint("ResourceAsColor")
class SettingPenPop(val context: Context,val selectInfo:(color:Int,lineWidth:Int)->Unit) : BaseBottomPopup(context) {
    var selectColor = R.color.app_bg_D9021B
    var lineWidth = 2
    val apt by lazy {
        BaseAdapter<Int, JyzsItemColorSelectBinding>(
            R.layout.jyzs_item_color_select, arrayListOf(
                R.color.app_bg_D9021B,
                R.color.app_bg_FFFF80,
                R.color.app_bg_0201FF,
                R.color.app_bg_02A7F0,
                R.color.app_bg_8080FF,
                R.color.app_bg_000000,
                R.color.app_bg_8401FF,
            )
        ).apply {
            onBind { itemBingding, position, data ->
                itemBingding.viewCircleRing.isVisible = (data == selectColor)
                val drawable = DrawableCreator.Builder().setSolidColor(context.color(data))
                    .setCornersRadius(dipToPx(20f)).build()
                itemBingding.viewColor.background = drawable
            }
            setOnItemClickListener { position, data ->
                val indexOf = datas.indexOf(selectColor)
                selectColor = data
                notifyItemChanged(indexOf)
                notifyItemChanged(position)
            }
        }
    }

    override fun getViews() = R.layout.jyzs_setting_pop

    init {
        findViewById<RecyclerView>(R.id.rv_color_select).apply {
            adapter = apt
            layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
            addItemDecoration(DividerItemDecoration(16f))
        }
        findViewById<RadioGroup>(R.id.rg_line_width).setOnCheckedChangeListener { group, checkedId ->
            when(checkedId){
                R.id.rb_s->{
                    lineWidth = 2
                }
                R.id.rb_m->{
                    lineWidth = 4
                }
                R.id.rb_l->{
                    lineWidth = 6
                }
            }
        }

    }

    override fun onDismiss() {
        super.onDismiss()
        selectInfo.invoke(selectColor,lineWidth)
    }
}